// pages/orderInfo/orderInfo.js
const API = require('../../API/api.js');
const app = getApp();
import Dialog from '../../utils/vant-weapp/dialog/dialog';

Page({
    getPrice: function (num) {
        var salesPrice = 0,
            totalPrice = 0,
            couponPrice = 0,
            discoutLimt = null,
            finalPrice = 0;
        //请求商品详情
        API.getGoodsDetails({
            id: this.data.goodsId,
            userId: wx.getStorageSync('userId')
        }).then((res) => {
            console.log(res)
            salesPrice = res.data.list[0].salesPrice;
            totalPrice = (salesPrice * num).toFixed(2);
            couponPrice = (totalPrice * discoutLimt).toFixed(2);
            finalPrice = (totalPrice - couponPrice).toFixed(2)
            this.setData({
                oderDetail: res.data.list[0],
                name: res.data.name,
                salesPrice: salesPrice,
                totalPrice: totalPrice,
                couponPrice: couponPrice,
                finalPrice: finalPrice,
                goodsSkuId: this.data.goodsSkuId
            })
        })
    },
    //添加地址
    goAddAddress() {
        wx.navigateTo({
            url: '../my/myInfo_address/myInfo_address?addAddress=1',
        })
    },
    price: function () {

    },
    onChange(event) {
        this.setData({
            radio: event.detail
        });
    },
    onChangeNum(e) {
        this.setData({
            num: e.detail
        })
        this.getPrice(this.data.num)
    },
    onClick(event) {
        const {
            name
        } = event.currentTarget.dataset;
        this.setData({
            radio: name
        });
    },
    remark(e) {
        console.log(e)
        this.setData({
            remark: e.detail.value
        })
    },
    pay() {
        //支付后台获取订单号
        API.pay({
            userId: wx.getStorageSync('userId'),
            goodsId: this.data.goodsId,
            goodsSkuId: this.data.goodsSkuId,
            addressId: this.data.addressId,
            num: this.data.num,
            remark: this.data.remark ? this.data.remark : null,
            payType: 1,
            sendType: 1
        }).then((res) => {
            console.log(res)
            var orderId = res.data;
            var openId = wx.getStorageSync('token').openid;
            //下单调用微信支付
            API.order({
                orderId: orderId,
                openId: openId
            }).then((res) => {
                console.log(res)
                if (res.code == 0) {
                    return Dialog.alert({
                        title: '提示',
                        message: res.msg
                    }).then(() => {
                        // on close
                    });
                }
                wx.requestPayment({
                    'timeStamp': res.data.timeStamp,
                    'nonceStr': res.data.nonceStr,
                    'package': res.data.package,
                    'signType': 'MD5',
                    'paySign': res.data.paySign,
                    'success': function (res) {
                        console.log(res)
                        wx.navigateTo({
                            url: '../payResult/payResult?orderId=' + orderId + '&payState=1',
                        })
                    },
                    'fail': function (res) {
                        console.log(res)
                        wx.navigateTo({
                            url: '../payResult/payResult?orderId=' + orderId + '&payState=0',
                        })
                    }
                })
            })

        })
    },
    /**
     * 页面的初始数据
     */
    data: {
        Height: app.globalData.Height,
        imgUrl: app.globalData.imgUrl,
        oderDetail: {},
        addressList: null,
        addressId: null,
        radio: '1',
        name: null,
        num: 1,
        goodsId: null,
        goodsSkuId: null,
        salesPrice: 0,
        totalPrice: 0,
        couponPrice: 0,
        discoutLimt: null,
        finalPrice: 0,
        remark: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options.goodsId)
        console.log(options.skuId)
        if (options.goodsId || options.skuId) {
            wx.setStorageSync('goodsId', options.goodsId)
            wx.setStorageSync('skuId', options.skuId)
            this.setData({
                goodsId: options.goodsId,
                goodsSkuId: options.skuId
            })
        }
        console.log(options.addressId)
        if (options.addressId) {
            API.getAddressInfo({
                userId: wx.getStorageSync('userId'),
                addressId: options.addressId
            }).then((res) => {
                this.setData({
                    addressList: res.data,
                })
            })

        }
        //请求地址列表
        API.getAddressList({
            userId: wx.getStorageSync('userId')
        }).then((res) => {
            console.log(res);
            for (let i = 0; i < res.data.length; i++) {
                if (res.data[i].isMain == 1) {
                    return this.setData({
                        addressList: res.data[i],
                        addressId: res.data[i].id,
                        goodsId: Number(options.goodsId),
                    })
                }
            }

        })
        //获取用户信息
        API.getMyUserInfo({
            userId: 123
        }).then((res) => {
            if (res.data.level == 0) {
                return
            } else if (res.data.level == 1) {
                discoutLimt: 0.18
            } else if (res.data.level == 2) {
                discoutLimt: 0.28
            } else if (res.data.level == 3) {
                discoutLimt: 0.36
            } else if (res.data.level == 4) {
                discoutLimt: 0.42
            }
        }).then(() => {
            this.getPrice(this.data.num)
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})
